import { request } from '@/service/request';

/**
 * DolphinScheduler同步配置
 */
export interface DsSyncConfig {
  id?: number;
  configName: string;
  syncType: string;
  syncMode: string;
  syncInterval?: number;
  enabled: number;
  lastSyncTime?: string;
  syncStatus?: string;
  errorMessage?: string;
  configParams?: string;
  remark?: string;
  createTime?: string;
  updateTime?: string;
}

/**
 * DolphinScheduler同步日志
 */
export interface DsSyncLog {
  id: number;
  configId?: number;
  syncType: string;
  syncAction: string;
  sourceId?: number;
  targetId?: number;
  syncStatus: string;
  syncResult?: string;
  errorMessage?: string;
  startTime?: string;
  endTime?: string;
  duration?: number;
  createTime: string;
}

/**
 * DolphinScheduler任务映射
 */
export interface DsTaskMapping {
  id: number;
  platformTaskId: number;
  platformTaskType: string;
  platformTaskName?: string;
  dsProjectCode?: number;
  dsWorkflowCode?: number;
  dsWorkflowName?: string;
  dsTaskCode?: number;
  dsTaskName?: string;
  mappingStatus: string;
  syncStatus?: string;
  lastSyncTime?: string;
  createTime?: string;
}

/**
 * 创建同步配置
 */
export function createSyncConfig(data: DsSyncConfig) {
  return request<number>({
    url: '/dataplatform/ds-sync/config',
    method: 'post',
    data
  });
}

/**
 * 更新同步配置
 */
export function updateSyncConfig(data: DsSyncConfig) {
  return request<boolean>({
    url: '/dataplatform/ds-sync/config',
    method: 'put',
    data
  });
}

/**
 * 删除同步配置
 */
export function deleteSyncConfig(id: number) {
  return request<boolean>({
    url: `/dataplatform/ds-sync/config/${id}`,
    method: 'delete'
  });
}

/**
 * 查询同步配置列表
 */
export function getSyncConfigList(params: any) {
  return request<any>({
    url: '/dataplatform/ds-sync/config/list',
    method: 'get',
    params
  });
}

/**
 * 手动触发同步
 */
export function triggerSync(configId: number) {
  return request<boolean>({
    url: `/dataplatform/ds-sync/trigger/${configId}`,
    method: 'post'
  });
}

/**
 * 创建ETL任务映射
 */
export function createEtlTaskMapping(etlTaskId: number) {
  return request<number>({
    url: `/dataplatform/ds-sync/mapping/etl/${etlTaskId}`,
    method: 'post'
  });
}

/**
 * 同步工作流状态
 */
export function syncWorkflowStatus(workflowCode: number) {
  return request<boolean>({
    url: `/dataplatform/ds-sync/workflow/${workflowCode}/sync`,
    method: 'post'
  });
}

/**
 * 同步实例状态
 */
export function syncInstanceStatus(instanceId: number) {
  return request<boolean>({
    url: `/dataplatform/ds-sync/instance/${instanceId}/sync`,
    method: 'post'
  });
}

/**
 * 查询任务映射关系
 */
export function getTaskMapping(platformTaskId: number, platformTaskType: string) {
  return request<DsTaskMapping>({
    url: `/dataplatform/ds-sync/mapping/${platformTaskId}/${platformTaskType}`,
    method: 'get'
  });
}

/**
 * 查询任务映射列表
 */
export function getTaskMappingList(params: any) {
  return request<any>({
    url: '/dataplatform/ds-sync/mapping/list',
    method: 'get',
    params
  });
}

/**
 * 查询同步日志
 */
export function getSyncLogs(params: any) {
  return request<any>({
    url: '/dataplatform/ds-sync/logs',
    method: 'get',
    params
  });
}

/**
 * 构建数据血缘
 */
export function buildDataLineage(taskId: number) {
  return request<boolean>({
    url: `/dataplatform/ds-sync/lineage/${taskId}`,
    method: 'post'
  });
}
